using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Controls;
using FontFamily = System.Windows.Media.FontFamily;
using System.Windows.Media;
using System.Runtime.InteropServices;

namespace InnerFireEditor {
  internal static class IconManager {

    public static FrameworkElement GetFolderIconControl() {
      // 使用方案3的字体图标实现
      return new TextBlock {
        Text = "\uE8B7",
        FontFamily = new FontFamily("Segoe MDL2 Assets"),
        FontSize = 16,
        Margin = new Thickness(0, 0, 5, 0)
      };
    }

    public static FrameworkElement GetPythonIconControl() {
      // 尝试使用 Segoe MDL2 Assets 字体中的 Python 图标（\uF3E2 是代码图标，作为替代）
      var icon = new TextBlock {
        Text = "\uF3E2", // 默认使用代码图标（若找不到Python专属图标）
        FontFamily = new FontFamily("Segoe MDL2 Assets"),
        FontSize = 16,
        Margin = new Thickness(0, 0, 5, 0),
        ToolTip = "Python 可执行文件"
      };

      // 检查是否支持 Python 专属图标（需系统版本支持）
      try {
        var font = new FontFamily("Segoe Fluent Icons"); // Win11 新字体
        if (IsFontIconSupported("\uEB4C", font)) // Python 图标 Unicode
        {
          icon.Text = "\uEB4C";
          icon.FontFamily = font;
        }
      } catch {
        // 回退到默认代码图标
      }

      return icon;
    }

    private static bool IsFontIconSupported(string iconUnicode, FontFamily font) {
      var typeface = new Typeface(font, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
      if (!typeface.TryGetGlyphTypeface(out var glyph))
        return false;

      ushort glyphIndex;
      return glyph.CharacterToGlyphMap.TryGetValue(iconUnicode[0], out glyphIndex);
    }

    public static System.Windows.Controls.Image GetFileIconControl(string filePath) {
      try {
        var icon = Icon.ExtractAssociatedIcon(filePath);
        var imageSource = Imaging.CreateBitmapSourceFromHIcon(
            icon.Handle,
            Int32Rect.Empty,
            BitmapSizeOptions.FromEmptyOptions());

        return new System.Windows.Controls.Image {
          Source = imageSource,
          Width = 16,
          Height = 16,
          Margin = new Thickness(0, 0, 5, 0)
        };
      } catch {
        return null;
      }
    }



    // 使用SHGetFileInfo API获取系统文件夹图标
    [DllImport("shell32.dll")]
    private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes,
      ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);

    [StructLayout(LayoutKind.Sequential)]
    private struct SHFILEINFO {
      public IntPtr hIcon;
      public IntPtr iIcon;
      public uint dwAttributes;
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
      public string szDisplayName;
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
      public string szTypeName;
    }

    private const uint SHGFI_ICON = 0x100;
    private const uint SHGFI_SMALLICON = 0x1;

    public static Icon GetFolderIcon() {
      SHFILEINFO shinfo = new SHFILEINFO();
      SHGetFileInfo("", 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON);
      return Icon.FromHandle(shinfo.hIcon);
    }
  }
}
